package com.carmanage.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.carmanage.dao.ActionDao;
import com.carmanage.dao.RoleDao;
import com.carmanage.domain.Action;
import com.carmanage.domain.Role;


@Repository
public class ActionDaoImpl implements ActionDao {
   
	
	private SessionFactory sessionFactory;
    private RoleDao roleDao;
	
    @Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
    
	public RoleDao getRoleDao() {
		return roleDao;
	}
	@Autowired
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public TreeSet<Integer> findActionidByRoleid(int roleid) {
		// TODO Auto-generated method stub
		Role role = roleDao.findRoleByRoleId(roleid);
		System.out.println("用户的登录的角色："+role);
		Iterator<Action> itera= role.getActions().iterator();
		TreeSet<Integer> TreeSetUserActionsId = new TreeSet<Integer>();
		while(itera.hasNext()){
			Integer i = (Integer)itera.next().getActionid();
		    TreeSetUserActionsId.add(i);
		}
		return TreeSetUserActionsId;
	}
    
	public List<Action> findAllAction(){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Action a");
		List<Action> ListActions = query.list();
		session.close();
		return ListActions;
	}

	@Override
	public Set<Integer> findActionColumnId(TreeSet<Integer> treeSetUserActionsId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Set<Integer> SetUserActionColumnId = new TreeSet<Integer>();
		Iterator<Integer> itera = treeSetUserActionsId.iterator();
		while(itera.hasNext()){
			Action action = (Action)session.get(Action.class,itera.next());
			Integer ActionColumnId = action.getActionColumn().getActioncolumnid();
			SetUserActionColumnId.add(ActionColumnId);
		}
		session.close();
		return SetUserActionColumnId;
	}
}
